#include "Shop.h"
#include "CMainApp.h"
#include "CGame.h"



Shop::Shop( CScene** ppScene, Main* pPerScene )
{
	m_pPerScene		= pPerScene;
	CScene::m_pPerScene	= pPerScene;
	m_ppScene		= ppScene;
	m_bPassword		= false;

}



void Shop::InitScene()
{
	m_mapSprObj.clear();
	g_CResource.PlaySound( SOUND_SHOP_IN );

	m_Hammer.InitHammer( m_pPerScene->m_Hammer.m_dwHammerID, m_pPerScene->m_Hammer.m_bQuickHammer );

	m_sprBackGround.CreateScript( SPR_TYPE_SCENE );
	m_sprBackGround.AddObject( CREATE_IMAGE_PKG_KEY( 0, 2, 0 ) );

	m_mapSprObj[ITEM_HEART_1].CreateScript( SPR_TYPE_DEFAULT_CC, 135, 460 );
	m_mapSprObj[ITEM_HEART_1].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 1 ) );
	g_CInputData.AddInputObject( ITEM_HEART_1, m_mapSprObj[ITEM_HEART_1].GetScriptRect() );
	m_Hammer.m_spr[0].SetPosX( m_mapSprObj[ITEM_HEART_1].GetPosX() );
	m_Hammer.m_spr[0].SetPosY( m_mapSprObj[ITEM_HEART_1].GetPosY() );

	m_mapSprObj[ITEM_HEART_2].CreateScript( SPR_TYPE_DEFAULT_CC, 240, 495 );
	m_mapSprObj[ITEM_HEART_2].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 2 ) );
	g_CInputData.AddInputObject( ITEM_HEART_2, m_mapSprObj[ITEM_HEART_2].GetScriptRect() );
	m_Hammer.m_spr[1].SetPosX( m_mapSprObj[ITEM_HEART_2].GetPosX() );
	m_Hammer.m_spr[1].SetPosY( m_mapSprObj[ITEM_HEART_2].GetPosY() );

	m_mapSprObj[ITEM_HEART_3].CreateScript( SPR_TYPE_DEFAULT_CC, 350, 460 );
	m_mapSprObj[ITEM_HEART_3].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 3 ) );
	g_CInputData.AddInputObject( ITEM_HEART_3, m_mapSprObj[ITEM_HEART_3].GetScriptRect() );
	m_Hammer.m_spr[2].SetPosX( m_mapSprObj[ITEM_HEART_3].GetPosX() );
	m_Hammer.m_spr[2].SetPosY( m_mapSprObj[ITEM_HEART_3].GetPosY() );

	m_mapSprObj[ITEM_WINGS].CreateScript( SPR_TYPE_DEFAULT_CC, 95, 350 );
	m_mapSprObj[ITEM_WINGS].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 4 ) );
	g_CInputData.AddInputObject( ITEM_WINGS, m_mapSprObj[ITEM_WINGS].GetScriptRect() );
	m_Hammer.m_spr[3].SetPosX( m_mapSprObj[ITEM_WINGS].GetPosX() );
	m_Hammer.m_spr[3].SetPosY( m_mapSprObj[ITEM_WINGS].GetPosY() );

	if( m_pPerScene->m_Hammer.m_bQuickHammer )
		m_mapSprObj[ITEM_WINGS].SetAction( SoldOut );
	
	if( ::Random() % ( 30 - m_pPerScene->m_dwLevel ) == 0 )
	{
		if( !g_CGame.CheckPassword( 1 ) )
		{
			m_bPassword = true;
			m_mapSprObj[ITEM_PASSWORD].CreateScript( SPR_TYPE_DEFAULT_CC, 240, 350 );
			m_mapSprObj[ITEM_PASSWORD].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 5 ) );
			g_CInputData.AddInputObject( ITEM_PASSWORD, m_mapSprObj[ITEM_PASSWORD].GetScriptRect() );

		}

	}

	m_mapSprObj[ITEM_HEART_MAX].CreateScript( SPR_TYPE_DEFAULT_CC, 390, 350 );
	m_mapSprObj[ITEM_HEART_MAX].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 6 ) );
	g_CInputData.AddInputObject( ITEM_HEART_MAX, m_mapSprObj[ITEM_HEART_MAX].GetScriptRect() );
	m_Hammer.m_spr[5].SetPosX( m_mapSprObj[ITEM_HEART_MAX].GetPosX() );
	m_Hammer.m_spr[5].SetPosY( m_mapSprObj[ITEM_HEART_MAX].GetPosY() );

	if( m_pPerScene->m_nMaxHeart == 6 )
		m_mapSprObj[ITEM_HEART_MAX].SetAction( SoldOut );

	m_mapSprObj[ITEM_HAMMER_1].CreateScript( SPR_TYPE_DEFAULT_CC, 135, 245 );
	m_mapSprObj[ITEM_HAMMER_1].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 7 ) );
	g_CInputData.AddInputObject( ITEM_HAMMER_1, m_mapSprObj[ITEM_HAMMER_1].GetScriptRect() );
	m_Hammer.m_spr[6].SetPosX( m_mapSprObj[ITEM_HAMMER_1].GetPosX() );
	m_Hammer.m_spr[6].SetPosY( m_mapSprObj[ITEM_HAMMER_1].GetPosY() );

	m_mapSprObj[ITEM_HAMMER_2].CreateScript( SPR_TYPE_DEFAULT_CC, 240, 205 );
	m_mapSprObj[ITEM_HAMMER_2].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 8 ) );
	g_CInputData.AddInputObject( ITEM_HAMMER_2, m_mapSprObj[ITEM_HAMMER_2].GetScriptRect() );
	m_Hammer.m_spr[7].SetPosX( m_mapSprObj[ITEM_HAMMER_2].GetPosX() );
	m_Hammer.m_spr[7].SetPosY( m_mapSprObj[ITEM_HAMMER_2].GetPosY() );

	m_mapSprObj[ITEM_HAMMER_3].CreateScript( SPR_TYPE_DEFAULT_CC, 350, 245 );
	m_mapSprObj[ITEM_HAMMER_3].AddObject( CREATE_IMAGE_PKG_KEY( 3, 1, 9 ) );
	g_CInputData.AddInputObject( ITEM_HAMMER_3, m_mapSprObj[ITEM_HAMMER_3].GetScriptRect() );
	m_Hammer.m_spr[8].SetPosX( m_mapSprObj[ITEM_HAMMER_3].GetPosX() );
	m_Hammer.m_spr[8].SetPosY( m_mapSprObj[ITEM_HAMMER_3].GetPosY() );

	m_mapSprObj[BTN_EXIT].CreateScript( SPR_TYPE_DEFAULT_CB, 360, 625 );
	m_mapSprObj[BTN_EXIT].AddObject( CREATE_IMAGE_PKG_KEY( 1, 0, 2 ) );
	g_CInputData.AddInputObject( BTN_EXIT, m_mapSprObj[BTN_EXIT].GetScriptRect() );

}



void Shop::RenderScene()
{
	m_sprBackGround.RenderScript();

	m_mapSprObj[ITEM_HEART_1].RenderScript();
	m_mapSprObj[ITEM_HEART_2].RenderScript();
	m_mapSprObj[ITEM_HEART_3].RenderScript();
	m_mapSprObj[ITEM_WINGS].RenderScript();
	if( m_bPassword )
		m_mapSprObj[ITEM_PASSWORD].RenderScript();
	m_mapSprObj[ITEM_HEART_MAX].RenderScript();
	m_mapSprObj[ITEM_HAMMER_1].RenderScript();
	m_mapSprObj[ITEM_HAMMER_2].RenderScript();
	m_mapSprObj[ITEM_HAMMER_3].RenderScript();
	m_mapSprObj[BTN_EXIT].RenderScript();

	m_Hammer.RenderHammer();
	m_pPerScene->RenderHP();
	m_pPerScene->RenderLevel();
	m_pPerScene->RenderMoney();
	m_pPerScene->RenderScore();
	m_pPerScene->m_Hammer.RenderHammerInfo();

}



void Shop::RunLogic()
{
	if( m_bLogicStart )
	{
		m_Hammer.RunLogic();

		if( !m_Hammer.IsHitting() )
		{
			if( g_CInputData.m_mapInputObject[ITEM_HEART_1].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_HEART_1 - 100 );
				if( m_pPerScene->m_dwMoney >= 50 && m_pPerScene->m_nCurHP < m_pPerScene->m_nMaxHeart * DEFAULT_HEART_HP )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 50;
					m_pPerScene->ReduceHP( -DEFAULT_HEART_HP );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( g_CInputData.m_mapInputObject[ITEM_HEART_2].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_HEART_2 - 100 );
				if( m_pPerScene->m_dwMoney >= 90 && m_pPerScene->m_nCurHP < m_pPerScene->m_nMaxHeart * DEFAULT_HEART_HP )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 90;
					m_pPerScene->ReduceHP( -( DEFAULT_HEART_HP << 1 ) );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( g_CInputData.m_mapInputObject[ITEM_HEART_3].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_HEART_3 - 100 );
				if( m_pPerScene->m_dwMoney >= 120 && m_pPerScene->m_nCurHP < m_pPerScene->m_nMaxHeart * DEFAULT_HEART_HP )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 120;
					m_pPerScene->ReduceHP( -( DEFAULT_HEART_HP * 3 ) );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( g_CInputData.m_mapInputObject[ITEM_WINGS].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_WINGS - 100 );
				if( m_pPerScene->m_dwMoney >= 80 && m_mapSprObj[ITEM_WINGS].GetCurActionID() != SoldOut )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 80;
					m_pPerScene->m_Hammer.ChangeHammer( m_pPerScene->m_Hammer.m_dwHammerID, true );
					m_Hammer.ChangeHammer( m_pPerScene->m_Hammer.m_dwHammerID, true );
					m_mapSprObj[ITEM_WINGS].SetAction( SoldOut );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( m_bPassword && g_CInputData.m_mapInputObject[ITEM_PASSWORD].m_nState == BTN_STATE_DOWN )
			{
				g_CResource.PlaySound( SOUND_BUY );

				if( !g_CGame.CheckPassword( 1 ) )
				{
					g_CGame.GetSetting()->dwPassword1 = g_CGame.CalPassword( 1 );
					g_CGame.SaveSetting();

				}

				m_bPassword = false;

			}
		//	else if( g_CInputData.m_mapInputObject[ITEM_LIFE].m_nState == BTN_STATE_DOWN )
		//	{
		//	}
			else if( g_CInputData.m_mapInputObject[ITEM_HEART_MAX].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_HEART_MAX - 100 );
				if( m_pPerScene->m_dwMoney >= 150 && m_mapSprObj[ITEM_HEART_MAX].GetCurActionID() != SoldOut )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 150;
					++m_pPerScene->m_nMaxHeart;
					m_pPerScene->ReduceHP( -DEFAULT_HEART_HP );

					if( m_pPerScene->m_nMaxHeart >= DEFAULT_MAX_HEART )
						m_mapSprObj[ITEM_HEART_MAX].SetAction( SoldOut );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( g_CInputData.m_mapInputObject[ITEM_HAMMER_1].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_HAMMER_1 - 100 );
				if( m_pPerScene->m_dwMoney >= 150 )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 150;
					m_pPerScene->m_Hammer.ChangeHammer( HAMMER_BRONZE, m_pPerScene->m_Hammer.m_bQuickHammer );
					m_Hammer.ChangeHammer( HAMMER_BRONZE, m_pPerScene->m_Hammer.m_bQuickHammer );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( g_CInputData.m_mapInputObject[ITEM_HAMMER_2].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_HAMMER_2 - 100 );
				if( m_pPerScene->m_dwMoney >= 200 )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 200;
					m_pPerScene->m_Hammer.ChangeHammer( HAMMER_SILVER, m_pPerScene->m_Hammer.m_bQuickHammer );
					m_Hammer.ChangeHammer( HAMMER_SILVER, m_pPerScene->m_Hammer.m_bQuickHammer );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( g_CInputData.m_mapInputObject[ITEM_HAMMER_3].m_nState == BTN_STATE_DOWN )
			{
				m_Hammer.Hit( ITEM_HAMMER_3 - 100 );
				if( m_pPerScene->m_dwMoney >= 300 )
				{
					g_CResource.PlaySound( SOUND_BUY );
					m_pPerScene->m_dwMoney -= 300;
					m_pPerScene->m_Hammer.ChangeHammer( HAMMER_GOLDEN, m_pPerScene->m_Hammer.m_bQuickHammer );
					m_Hammer.ChangeHammer( HAMMER_GOLDEN, m_pPerScene->m_Hammer.m_bQuickHammer );

				}
				else
					g_CResource.PlaySound( SOUND_NO_BUY );

			}
			else if( g_CInputData.m_mapInputObject[BTN_EXIT].m_nState == BTN_STATE_DOWN )
			{
				g_CResource.PlaySound( SOUND_PUSH );

				for( int index = 0; index != 9; ++index )
				{
					if( m_pPerScene->m_pLevelParam->dwBoss &&
						index == BOSS_SQUARE )
						continue ;

					m_pPerScene->m_Square[index].SetToBottom();

				}

				m_pPerScene->SetAnimate( ANIME_FADE_OUT, ANIME_CMD_START_LOGIC );
				SetAnimate( ANIME_FADE_IN, ANIME_CMD_PER_SCENE | ANIME_CMD_DEL_THIS );
				return ;

			}

		}

	}

}